package along.com.myboxugu.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import along.com.myboxugu.R;

public class LoginActivity extends AppCompatActivity {
    private EditText musername;
    private EditText mpassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
      mpassword= (EditText) findViewById(R.id.password);
       musername= (EditText) findViewById(R.id.username);
    }
  public void login(View view){
      RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Login");
      params.addQueryStringParameter("username", musername.getText().toString());
      params.addQueryStringParameter("password", mpassword.getText().toString());
      x.http().get(params, new Callback.CommonCallback<String>() {  //
          @Override
          public void onSuccess(String result) {
              Gson gson =new Gson();  //Gson解析json（LoginModel）文件
              LoginModel loginModel = gson.fromJson(result,LoginModel.class);
              if(loginModel.code==200){
                  //保存登陆状态 1：文件保存 2：数据库保存SharedPreferences
                  //name 保存文件名称;
                  SharedPreferences sp = getSharedPreferences("login", Context.MODE_APPEND);//别的进程读不到
                  SharedPreferences.Editor editor =sp.edit();
                  editor.putBoolean("islogin",true);
                  editor.putString("username",musername.getText().toString());
                  editor.commit();
                  finish();
              }
              else {
                      Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
          }

        Log.i("LoginActivity","onSuccess result"+result);
          }
          @Override
          public void onError(Throwable ex, boolean isOnCallback) {
              Log.i("LoginActivity","onError result"+ex.getMessage());

          }

          @Override
          public void onCancelled(CancelledException cex) {

          }

          @Override
          public void onFinished() {

          }
      });
  }
    public void back(View view){
      finish();
     }
    public void immediately(View view){
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);

    }
  }



